package com.peace.hospital.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.peace.hospital.entity.hospitalization.entity.Inpatientmessage;
import com.peace.hospital.entity.hospitalization.entity.People;
import com.peace.hospital.entity.hospitalization.entity.dto.AllchargeDto;
import org.apache.ibatis.annotations.*;

import java.util.List;


/**
 * <p>
 * 入库单表和药房表（多对多） Mapper 接口
 * </p>
 *
 * @author xie
 * @since 2019-11-06
 */
@Mapper
public interface InpatientmessageMapper extends BaseMapper<Inpatientmessage> {



    /**
     * 检验是否存在该病人当前住院记录
     * @return
     */
    @Select({"SELECT * " +
            "FROM outpatientdepartment.inpatientmessage "+
            "WHERE inpleavehos IS NULL and pid = #{pid}"})
    Inpatientmessage selectInpatientMessage(People people);


    /**
     * 添加住院信息
     * @param i
     * @return
     */
    @Insert({
            "INSERT INTO outpatientdepartment.inpatientmessage(staid,sckid,pid,inpadmdate,inpcontact,inpleavehos,inpdeposit,inpdbsource) " +
                    "VALUES(#{satid},#{sckid},#{pid},NOW(),#{inpcontact},'',#{inpdeposit},'hospital' )"
    })
    int insertInner(Inpatientmessage i);

    @Select({
            "SELECT inpid,i.pid,pname,inpcontact,inpdeposit " +
                    "FROM hospital_public.people p INNER JOIN outpatientdepartment.inpatientmessage i ON p.pid = i.pid " +
                    "inner join hospital_public.staffinfo sta on sta.staid=i.staid "+
                    "WHERE i.inpleavehos IS NULL OR i.inpleavehos = '' and depid = #{depid}"
    })
    @Results({

            @Result(property = "inpid",column = "inpid"),
            @Result(property = "pid",column = "p.pid"),
            @Result(property = "pname",column = "pname"),
            @Result(property = "inpcontact",column = "inpcontact"),
            @Result(property = "inpdeposit",column = "inpdeposit")
    })
    List<Inpatientmessage> getAll(Integer d);

    /**i
     *
     * 出院操作
     * @return
     */
    @Update({"UPDATE outpatientdepartment.inpatientmessage i " +
            "INNER JOIN outpatientdepartment.allcharge a ON " +
            "i.pid = a.pid SET inpleavehos = '出院' WHERE i.pid=#{pid} AND " +
            "(a.allprice - i.inpdeposit)<0 AND a.allsettlement = 1"})
    Integer updateOut(Inpatientmessage i);

    /**
     * 催款
     */
    @Select("SELECT i.pid, p.pname, i.inpdeposit, a.allprice, a.alldate FROM outpatientdepartment.inpatientmessage i " +
            "INNER JOIN hospital_public.allcharge a ON i.pid = a.pid " +
            "INNER JOIN hospital_public.people p ON i.pid = p.pid " +
            "WHERE (a.allprice - i.inpdeposit)>0")
    @Results({
            @Result(property = "people.pid",column = "i.pid"),
            @Result(property = "people.pname",column = "p.pname"),
            @Result(property = "inpatientmessage.inpdeposit",column = "i.inpdeposit"),
            @Result(property = "allcharge.allprice",column = "a.allprice"),
            @Result(property = "allcharge.alldate",column = "a.alldate")
    })
    List<AllchargeDto> selectDebts();
    /**
     * 住院病人信息修改
     * @param i-住院病人新信息
     * @return
     */
    @Update({
            "UPDATE outpatientdepartment.inpatientmessage SET " +
                    "inpcontact = #{inpcontact}," +
                    "inpdeposit = #{inpdeposit}" +
                    "WHERE inpid = #{inpid}"
    })
    Integer updateNewMessage(Inpatientmessage i);
}
